import { createRouter, createWebHashHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
import type { App } from 'vue'
import { Layout} from '@/utils/routerHelper'
import { useI18n } from '@/hooks/web/useI18n'

const { t } = useI18n()

export const constantRouterMap: AppRouteRecordRaw[] = [
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard/workplace',
    name: 'Root',
    meta: {
      hidden: true
    }
  },
  {
    path: '/redirect',
    component: Layout,
    name: 'Redirect',
    children: [
      {
        path: '/redirect/:path(.*)',
        name: 'Redirect',
        component: () => import('@/views/Redirect/Redirect.vue'),
        meta: {}
      }
    ],
    meta: {
      hidden: true,
      noTagsView: true
    }
  },
  {
    path: '/login',
    component: () => import('@/views/Login/Login.vue'),
    name: 'Login',
    meta: {
      hidden: true,
      title: t('router.login'),
      noTagsView: true
    }
  },
  {
    path: '/404',
    component: () => import('@/views/Error/404.vue'),
    name: 'NoFind',
    meta: {
      hidden: true,
      title: '404',
      noTagsView: true
    }
  }
]

export const asyncRouterMap: AppRouteRecordRaw[] = [
  // {
  //   path: '/',
  //   component: Layout,
  //   name: '首页',
  //   meta: {
  //     title: t('首页'),
  //   },
  //   children: [
  //     {
  //       path: '/',
  //       name: '首页',
  //       component: () => import('@/views/Home/index.vue'),
  //       meta: {
  //         title: t('首页'),
  //         icon: 'ep:home-filled'
  //       }
  //     }
  //   ],
  // },
  {
    path: '/dashboard',
    component: Layout,
    redirect: '/dashboard/workplace',
    name: 'Dashboard',
    meta: {
      title: t('router.dashboard'),
      icon: 'ant-design:dashboard-filled',
      alwaysShow: true
    },
    children: [
      {
        path: 'workplace',
        component: () => import('@/views/Dashboard/Workplace.vue'),
        name: 'Workplace',
        meta: {
          title: t('router.workplace'),
          noCache: true,
          affix: true
        }
      },
      // {
      //   path: 'analysis',
      //   component: () => import('@/views/Dashboard/Analysis.vue'),
      //   name: 'Analysis',
      //   meta: {
      //     title: t('router.analysis'),
      //     noCache: true,
      //     affix: true
      //   }
      // },

    ]
  },
  {
    path: '/content-manage',
    component: Layout,
    redirect: '/content-manage/list',
    name: '内容管理',
    meta: {
      title: t('内容管理'),
      icon:'ep:notebook'
    },
    children: [
      {
        path: 'list',
        name: '文章列表',
        component: () => import('@/views/content-manage/index.vue'),
        meta: {
          title: t('文章列表'),
          icon: 'ep:document'
        }
      },
      {
        path: 'list-release',
        component: () => import('@/views/content-manage/release.vue'),
        name: '发布内容',
        meta: {
          title: t('发布内容'),
          hidden: true,
          noCache: true,
          noTagsView: true,
          canTo: true,
          activeMenu: '/content-manage/list-release'
        }
      },
      {
        path: 'list-edit',
        component: () => import('@/views/content-manage/release.vue'),
        name: '编辑内容',
        meta: {
          title: t('编辑内容'),
          hidden: true,
          noCache: true,
          noTagsView: true,
          canTo: true,
          activeMenu: '/content-manage/list-edit'
        }
      },
      {
        path: 'check',
        name: '文章审核',
        component: () => import('@/views/check-manage/index.vue'),
        meta: {
          title: t('文章审核'),
          icon: 'ep:zoom-out'
        }
      },
      {
        path: 'check-dispose',
        component: () => import('@/views/check-manage/check.vue'),
        name: '审核内容',
        meta: {
          title: t('审核内容'),
          hidden: true,
          noCache: true,
          canTo: true,
          activeMenu: '/content-manage/check-dispose'
        }
      },
      {
        path: 'column',
        name: '文章栏目',
        component: () => import('@/views/content-manage/column.vue'),
        meta: {
          title: t('文章栏目'),
          icon: 'ep:operation'
        }
      },
    ]
  },
  {
    path: '/system',
    component: Layout,
    redirect: '/system/user',
    name: '系统管理',
    meta: {
      title: t('系统管理'),
      icon: 'ep:grid'
    },
    children: [
      {
        path: 'user',
        name: '账号管理',
        component: () => import('@/views/system-manage/user/index.vue'),
        meta: {
          title: t('账号管理'),
          icon: 'ep:user-filled',
        },
      },
      {
        path: 'user-insert',
        component: () => import('@/views/system-manage/user/add.vue'),
        name: '新增账号',
        meta: {
          title: t('新增账号'),
          hidden: true,
          noCache: true,
          noTagsView: true,
          canTo: true,
          // activeMenu: '/system/user-insert'
        }
      },
      {
        path: 'user-edit',
        component: () => import('@/views/system-manage/user/add.vue'),
        name: '编辑账号',
        meta: {
          title: t('编辑账号'),
          hidden: true,
          noCache: true,
          noTagsView: true,
          canTo: true,
          activeMenu: '/system/user-edit'
        }
      },
      {
        path: 'user-info',
        component: () => import('@/views/system-manage/user/add.vue'),
        name: '账号详情',
        meta: {
          title: t('账号详情'),
          hidden: true,
          noCache: true,
          noTagsView: true,
          canTo: true,
          activeMenu: '/system/user-info'
        }
      },
      {
        path: 'role',
        name: '权限管理',
        component: () => import('@/views/system-manage/role/index.vue'),
        meta: {
          title: t('权限管理'),
          icon: 'ep:lock'
        },
      },
      {
        path: 'menu',
        name: '菜单管理',
        component: () => import('@/views/system-manage/menu/index.vue'),
        meta: {
          title: t('菜单管理'),
          icon: 'ep:menu'
        },
      },
    ]
  },
  {
    path: '/index-manage',
    component: Layout,
    redirect: '/index-manage/index',
    name: '首页管理',
    meta: {
      title: t('首页管理'),
    },
    children: [
      {
        path: 'index',
        name: '首页管理',
        component: () => import('@/views/index-manage/index.vue'),
        meta: {
          title: t('首页管理'),
          icon: 'ep:house'
        }
      },
    ],
  },
  {
    path: '/school-manage',
    component: Layout,
    redirect: '/school-manage/index',
    name: '学校管理',
    meta: {
      title: t('学校管理'),
      icon: 'ep:school'
    },
    children: [
      {
        path: 'index',
        name: '学校列表',
        component: () => import('@/views/school-manage/school-list/index.vue'),
        meta: {
          title: t('学校列表'),
        }
      },
      {
        path: 'school-insert',
        component: () => import('@/views/school-manage/school-list/info.vue'),
        name: '添加学校',
        meta: {
          title: t('添加学校'),
          hidden: true,
          noCache: true,
          noTagsView: true,
          canTo: true,
          activeMenu: '/school-manage/school-insert'
        }
      },
      {
        path: 'school-info',
        component: () => import('@/views/school-manage/school-list/info.vue'),
        name: '编辑学校',
        meta: {
          title: t('编辑学校'),
          hidden: true,
          noCache: true,
          noTagsView: true,
          canTo: true,
          activeMenu: '/school-manage/school-info'
        }
      },
      {
        path: 'column',
        name: '学校栏目',
        component: () => import('@/views/school-manage/school-column/index.vue'),
        meta: {
          title: t('学校栏目'),
        }
      },
    ],
  },
  {
    path: '/system-setting',
    component: Layout,
    redirect: '/system-setting/index',
    name: '项目设置',
    meta: {
      title: t('项目设置'),
    },
    children: [
      {
        path: 'index',
        name: '项目设置',
        component: () => import('@/views/system-setting/index.vue'),
        meta: {
          title: t('项目设置'),
          icon: 'ep:tools'
        }
      },
    ],
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  strict: true,
  routes: constantRouterMap as RouteRecordRaw[],
  scrollBehavior: () => ({ left: 0, top: 0 })
})

export const resetRouter = (): void => {
  const resetWhiteNameList = ['Redirect', 'Login', 'NoFind', 'Root']
  router.getRoutes().forEach((route) => {
    const { name } = route
    if (name && !resetWhiteNameList.includes(name as string)) {
      router.hasRoute(name) && router.removeRoute(name)
    }
  })
}

export const setupRouter = (app: App<Element>) => {
  app.use(router)
}

export default router
